//
//  WWTableContentCollectionCell.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/5.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWTableContentCollectionCell.h"
#import "WWContentTableCell.h"
#import "WWTableContentModel.h"
#import "PublicHeader.h"
#import <MBProgressHUD.h>

@interface WWTableContentCollectionCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)MBProgressHUD *progressView;

@end

@implementation WWTableContentCollectionCell

- (void)handleData:(NSMutableDictionary *)data {
    self.dataArray = [NSMutableArray array];
    NSArray *tempArr = [NSArray arrayWithArray:[data objectForKey:@"list"]];
    for (NSDictionary *dic in tempArr) {
        WWTableContentModel *tableModel = [WWTableContentModel wwModelWithDictionary:dic];
        [self.dataArray addObject:tableModel];
    }
    [self.tableView reloadData];
    [_progressView removeFromSuperview];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WWContentTableCell class] forCellReuseIdentifier:@"tableCell"];
        self.tableView.rowHeight = FitConstant_YorHeight(80);
        [self.contentView addSubview:_tableView];
        _progressView = [[MBProgressHUD alloc] initWithView:self.tableView];
        _progressView.labelText = @"努力加载中,请稍等";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
        [self.tableView addSubview:self.progressView];
        [self.tableView NightWithType:UIViewColorTypeNormal];
    }
    return self;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WWContentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell"];
    WWTableContentModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WWTableContentModel *model = self.dataArray[indexPath.row];
    NSString *aid = [NSString stringWithFormat:@"%@", model.albumId];
    NSString *uid = [NSString stringWithFormat:@"%@", model.uid];
    [self.conDelegate pass:uid withAlumbTitle:model.title withAlbumId:aid];
}

@end
